var app = getApp();
var named;
var pickerFile = require('../tools/js/picker_datetime.js');
var bmap = require('../../libs/bmap-wx.js');
var wxMarkerData = []; 
var ksf=0;
var array_add= [0, 0, 0];
var add_total=array_add[0]+array_add[1]+array_add[2];
var total_price1;
var buy_address;
var openid = app.globalData.openid;

Page({
  data: {
    showView: false,
    showView_1: false,
    index: 0,
    imgkey1:1,
    imgkey2: 2,
    imgkey3: 2,
    array: ['0.5kg', '1.0kg', '1.0~1.5kg', '1.5~2kg'],
    time_point:false,

    winHeight: "",//窗口高度
    currentTab: 0, //预设当前项的值
    scrollLeft: 0, //tab标题的滚动条位置
    address: '',
    expertList: [{ //假数据
      img: "avatar.png",
      name: "欢顔",
      tag: "知名情感博主",
      answer: 134,
      listen: 2234
    }],
    kinds:[{
      name:"文件/证件",
      select:"0",
    },{
      name: "日用品",
      select: "1",
    },{
      name: "文件",
      select: "2",
    },{
      name: "美食",
      select: "3",
    }],
    Naddress:[{
      name:"姜枫",
      phone:"15050836494",
      dizhi:"徐州市鼓楼区黄河北路店"
    }, {
      name: "张三",
      phone: "15050836494",
      dizhi: "徐州市鼓楼区黄河北路店"
      },{
        name: "李磊",
        phone: "15050836494",
        dizhi: "徐州市鼓楼区黄河北路店"
      }],
    Saddress: [
      {
      sname: "李四",
      sphone: "15636363636",
      sdizhi: "徐州市云龙区万达广场"
    },
      {
        sname: "取件人2",
        sphone: "15636363636",
        sdizhi: "徐州市云龙区万达广场"
      },
      {
        sname: "取件人3",
        sphone: "15636363636",
        sdizhi: "徐州市云龙区万达广场"
      }
    ],
    swplx:"文件/证件",
    swpzl:"0.5kg",
    sqjsj:"立即取件",
    qwplx:"日用品",
    qwpzl:"1.0kg",
    
    catalogSelect: 0,

    list: [
      {
        id: 'view',
        name: '动态加价',
        open: false,
        // pages: ['恶劣天气动态加价', '晚间加价', '节假日加价'],
        // imaurl: ['images/ware_1.png', 'images/ware_2.png', 'images/ware_3.png'],
      }
    ],
    markers: [],
    latitude: '',
    longitude: '',
    placeData: {} 

  },
  bindPickerChange: function (e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },
  // 滚动切换标签样式
  switchTab: function (e) {
    this.setData({
      currentTab: e.detail.current
    });
    this.checkCor();
    // console.log(e.detail.current);
  },
  buyadd:function(){
    wx.navigateTo({
      url: '/pages/menu/menu',
    })
  },
  // 点击标题切换当前页时改变样式
  swichNav: function (e) {
    console.log(e);
    var cur = e.target.dataset.current;
    if (this.data.currentTaB == cur) { return false; }
    else {
      this.setData({
        currentTab: cur
      })
    }
  },
  // 消息页面跳转
  message:function(){
    wx.navigateTo({
      url: '/pages/message/message',
    })
  },
  mypage:function(){
    wx.navigateTo({
      url: '/pages/mine/mine',
    })
  },

  //判断当前滚动超过一屏时，设置tab标题滚动条。
  checkCor: function () {
    if (this.data.currentTab > 4) {
      this.setData({
        scrollLeft: 300
      })
    } else {
      this.setData({
        scrollLeft: 0
      })
    }
  },

  makertap: function (e) {
    var that = this;
    var id = e.markerId;
    that.showSearchInfo(wxMarkerData, id);
    that.changeMarkerColor(wxMarkerData, id);
  }, 

  
  onLoad: function (options) {
    if (options.currentTab == null){
      var currentTab = 0;
    }else{
      var currentTab = options.currentTab;
    }
    var that = this;

    var options = options;
   
    // 发货地址的所有选项
    var name = options.name;
    var phone = options.mobile;
    var address_1 = options.address_1;
    var address_2 = options.address_2;
    
    var id=options.id;
    var buy_address = options.shopName;
    // var currentTab = options.currentTab;
    // wx.setStorageSync('name', name);
    // name: wx.getStorageSync('name')
    // wx.setStorageSync('use', openid)
    // openid: wx.getStorageSync('use')

    // 收货地址的所有选项
    var Sname = options.Sname;
    var Sphone = options.Smobile;
    var Saddress_1 = options.Saddress_1;
    var Saddress_2 = options.Saddress_2;
  
    // 时间日期选择
    this.datetimePicker = new pickerFile.pickerDatetime({
      page: this,
      animation: 'slide',
      duration: 500
    }); 
    // 日期选择结束
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;
        that.setData({
          ress:that.data.Naddress,
          ressslen: that.data.Naddress.length,
          ress2:that.data.Saddress,
          ressslen2: that.data.Saddress.length, 
          winHeight: calc
        });
      }
    });
    this.setData({
      // 发货地址的选项
      inputInform: name + phone,
      inputAddress: address_1 + address_2,
      // 收货地址的选项
      inputInform_2: Sname + Sphone,
      inputAddress_2: Saddress_1 + Saddress_2,
      buy_address: buy_address,
      currentTab: currentTab,
      imgkey1:1,
      imgkey2:2,
      imgkey3:2
    })

    var that = this;
    // 新建百度地图对象 
    var BMap = new bmap.BMapWX({
      ak: 'bzQsfmU86UsaqMOeZzXFrjbkVAUKcVYW'
    });
    var fail = function (data) {
      console.log(data)
    };
    var success = function (data) {  
      wxMarkerData = data.wxMarkerData;
      that.setData({
        markers: wxMarkerData
      });
      that.setData({
        latitude: wxMarkerData[0].latitude
      });
      that.setData({
        longitude: wxMarkerData[0].longitude
      });
    }
    // wx.request({
    //   url: '',
    //   data: currentTab,
    //   success:function(){
            1
     //}
    // }
    // })
    // 发起POI检索请求 
    BMap.search({
      "query": '酒店',
      fail: fail,
      success: success,
      // 此处需要在相应路径放置图片文件 
      iconPath: '../../img/marker_red.png',
      // 此处需要在相应路径放置图片文件 
      iconTapPath: '../../img/marker_red.png'
    });
  },
  showSearchInfo: function (data, i) {
    var that = this;
    that.setData({
      placeData: {
        title: '名称：' + data[i].title + '\n',
        address: '地址：' + data[i].address + '\n',
        telephone: '电话：' + data[i].telephone
      }
    });
  },
  changeMarkerColor: function (data, i) {
    var that = this;
    var markers = [];
    for (var j = 0; j < data.length; j++) {
      if (j == i) {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "../../img/marker_yellow.png";
      } else {
        // 此处需要在相应路径放置图片文件 
        data[j].iconPath = "../../img/marker_red.png";
      }
      markers[j](data[j]);
    }
    that.setData({
      markers: markers
    }); 



  },
  // 日期选择出发事件
  //送
  startTap1: function () {
    this.datetimePicker.setPicker('startDate1');
    this.setData({
      time_point:true
    })
  },
  //取
  startTap2: function () {
    this.datetimePicker.setPicker('startDate2');
    this.setData({
      time_point: true
    })
  },
  //买
  startTap3: function () {
    this.datetimePicker.setPicker('startDate3');
    this.setData({
      time_point: true
    })
  },
  // 第一栏发货地址点击
  Tosend1:function(mobile){
    wx.navigateTo({
      url: '/pages/send_info/send_info?id=1',
    })
  },
  Tosend2: function (mobile) {
    wx.navigateTo({
      url: '/pages/send_info/send_info?id=2',
    })
  },
  Tosend3: function (mobile) {
    wx.navigateTo({
      url: '/pages/send_info/send_info?id=3',
    })
  },
  // 第二栏收货地址点击
  Toget: function () {
    wx.navigateTo({
      url: '/pages/get_info/get_info',
    })
  },
  // 送的页面 ------ 物品类型选择弹整框
  goodsClass:function(e){
    var that = this;
    that.setData({
      showView: (!that.data.showView)
    })
  },
  money_rude:function(){
    var that = this;
    that.setData({
      showView_1: (!that.data.showView)
    })
  },
  // 送的页面----- 物品类型点击选择
  Chosinte:function(e){
    var that = this;
    named = e.currentTarget.dataset.name;
    this.setData({
      select: e.currentTarget.dataset.select,
      showView: (!that.data.showView),
      named:named
    })
  },
  // 点击灰色部分关闭
  closewidow:function(){
    var that = this;
    this.setData({
      showView: false,
      showView_1: false,
    })
  },
  Valued:function(){
    var that = this;
    this.setData({
      showView_1: (!that.data.showView_1),
    })
  },
  // 收叠
  kindToggle: function (e) {
    var id = e.currentTarget.id, list = this.data.list;
    for (var i = 0, len = list.length; i < len; ++i) {
      if (list[i].id == id) {
        list[i].open = !list[i].open
      } else {
        list[i].open = false
      }
    }
    this.setData({
      list: list
    });
  },
  footerTap: app.footerTap,

  onShow:function(options){
    var ceshishuzu=[5,2.5,0];
    var ksf1=12;
    var ksf2=15;
    var ksf3=10;
    var amount= amount();
    var price_total_song = parseFloat(ksf1)+parseFloat(amount);
    var price_total_qu = parseFloat(ksf2) + parseFloat(amount);
    var price_total_mai = parseFloat(ksf3) + parseFloat(amount);
    this.setData({
      ksf1:parseFloat(ksf1).toFixed(2),
      ksf2: parseFloat(ksf2).toFixed(2),
      ksf3: parseFloat(ksf3).toFixed(2),
      array_add: [parseFloat(ceshishuzu[0]).toFixed(2), parseFloat(ceshishuzu[1]).toFixed(2), parseFloat(ceshishuzu[2]).toFixed(2)],
      add_total:amount,
      price_total1: parseFloat(price_total_song).toFixed(2),
      price_total2: parseFloat(price_total_qu).toFixed(2),
      price_total3: parseFloat(price_total_mai).toFixed(2),
    })
     
    function amount(){
        var add_total = ceshishuzu[0] + ceshishuzu[1] + ceshishuzu[2],
         add_total=parseFloat(add_total).toFixed(2);
      
        return add_total;
    }
  },
  
  //送--表单验证
  formSubmit1:function(e){
    console.log(e.detail.value);
   
    var fhxx_top = e.detail.value.s_fhxx_top;//送-发货信息1
    var fhxx_addr = e.detail.value.s_fhxx_addr;//送-发货信息2（发货地址）
    var shxx_top = e.detail.value.s_shxx_top;//送-收货信息1
    var shxx_addr = e.detail.value.s_shxx_addr;//送-收货信息2（收货地址）
    var wplx = e.detail.value.s_wplx;//送-物品类型
    var wpzl = e.detail.value.s_wpzl;//送-物品重量
    
    if (!fhxx_top|| !fhxx_addr) {
      wx.showToast({
        title: '请补充完整发货信息',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if(!shxx_top || !shxx_addr){
      wx.showToast({
        title: '请补充完整收货信息',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (!wplx) {
      wx.showToast({
        title: '请选择物品类型',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if(!wpzl){
      wx.showToast({
        title: '请选择物品重量',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
  if (fhxx_top && fhxx_addr && shxx_top && shxx_addr && wplx && wpzl) {
     
       wx.showToast({
         title: '提交成功',
         icon: 'success'
       })

       return true;
    }
 },

//取件--表单验证
  formSubmit2: function (e) {
    console.log(e.detail.value);

    var fhxx_top = e.detail.value.q_fhxx_top;//送-发货信息1
    var fhxx_addr = e.detail.value.q_fhxx_addr;//送-发货信息2（发货地址）
    var shxx_top = e.detail.value.q_shxx_top;//送-收货信息1
    var shxx_addr = e.detail.value.q_shxx_addr;//送-收货信息2（收货地址）
    var wplx = e.detail.value.q_wplx;//送-物品类型
    var wpzl = e.detail.value.q_wpzl;//送-物品重量

    if (!fhxx_top || !fhxx_addr) {
      wx.showToast({
        title: '请补充完整发货信息',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (!shxx_top || !shxx_addr) {
      wx.showToast({
        title: '请补充完整收货信息',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (!wplx) {
      wx.showToast({
        title: '请选择物品类型',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (!wpzl) {
      wx.showToast({
        title: '请选择物品重量',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (fhxx_top && fhxx_addr && shxx_top && shxx_addr && wplx && wpzl) {

      wx.showToast({
        title: '提交成功',
        icon: 'success'
      })

      return true;
    }
  },
  //买--表单验证
  formSubmit3: function (e) {
    console.log(e.detail.value);

    var fhxx_top = e.detail.value.m_fhxx_top;//买-发货信息1
    var fhxx_addr = e.detail.value.m_fhxx_addr;//买-发货信息2（发货地址）
    var wplx = e.detail.value.m_wplx;//买-物品类型
    var gmdd = e.detail.value.m_gmdd;//买-购买地点

    if (!fhxx_top || !fhxx_addr) {
      wx.showToast({
        title: '请补充完整发货信息',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    
    if (!wplx) {
      wx.showToast({
        title: '请选择物品类型',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (!gmdd) {
      wx.showToast({
        title: '请填写购买地点',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    if (fhxx_top && fhxx_addr && wplx && gmdd) {

      wx.showToast({
        title: '提交成功',
        icon: 'success'
      })

      return true;
    }
  },
  onReady:function(){
   
  }


 
  
})